package 双指针系列;

public class Remove_element {
    //回炉重造
    //写法一
    public int removeElement1(int[] nums, int val) {
        int n = nums.length;
        for(int i=0;i<n;i++){
            if(nums[i]==val){
                for(int j=i+1;j<n;j++){
                    nums[j-1] = nums[j];
                }
                i--;
                n--;
            }
        }
        return n;
    }
    //写法二
    public int removeElement2(int[] nums, int val) {
        int slowIndex = 0;
        int n = nums.length;
        for(int fastIndex = 0;fastIndex<n;fastIndex++){
            if(nums[fastIndex] != val){
                nums[slowIndex++] = nums[fastIndex];
            }
        }
        return slowIndex;
    }
}
